package com.fort.teachermanagementsys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fort.teachermanagementsys.dto.ClassQueryDTO;
import com.fort.teachermanagementsys.entity.ClassY;
import com.fort.teachermanagementsys.request.PageParamRequest;
import com.fort.teachermanagementsys.service.ClassManagementService;
import com.fort.teachermanagementsys.utils.Result;

import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classManagement")
@Slf4j
public class ClassManaagementController {


    @Autowired
    private ClassManagementService classManagementService;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @ApiOperation("新增班级")
    @PostMapping("/addClass")
    public Result<ClassY> addClass(@RequestBody ClassY c) {
        Result<ClassY> result = new Result();
        if (classManagementService.addClass(c)){
            result.setMsg("success");
            result.setCode(200);
            result.setDetail(c);
        }else {
            result.setMsg("fail");
            result.setCode(500);
        }
        return result;
    }

    @ApiOperation("获取班级列表")
    @GetMapping("getClassList")
    public Result<IPage<ClassY>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            ClassQueryDTO query) {
        log.info("收到请求: current={}, size={}, query={}", current, size, query);
        try {
            Page<ClassY> page = new Page<>(current, size);
            IPage<ClassY> result = classManagementService.getClassYPage(page, query);
            log.info("查询结果: total={}, records={}", result.getTotal(), result.getRecords());
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取班级列表失败: {}", e.getMessage(), e);
            return Result.failed("获取班级列表失败");
        }
    }


    @ApiOperation("根据班级 ID 获取详情")
    @GetMapping("/getClass/{id}")
    public Result<ClassY> getClassById(@PathVariable("id") Integer classId) {
        Result<ClassY> result = new Result<>();
        try {
            ClassY classY = classManagementService.getById(classId); // MyBatis Plus 的 getById 方法
            if (classY != null) {
                result.setMsg("success");
                result.setCode(200);
                result.setDetail(classY);
            } else {
                result.setMsg("班级不存在");
                result.setCode(404);
            }
        } catch (Exception e) {
            result.setMsg("获取班级详情失败: " + e.getMessage());
            result.setCode(500);
        }
        return result;
    }


}
